const qs = require('qs')
export function api(val: any) {
  let {
    path,
    method = "GET",
    body
  } = val;
  var myHeaders = new Headers();
  myHeaders.append('Content-Type', 'application/json; charset=utf-8');
  myHeaders.append('Accept', 'application/json')
  let options: any = {
    method,
    headers: myHeaders,
    body,
    mode: 'no-cors',
    cache: 'default'
  }
  if (method.toUpperCase() === "GET" && body) {
    path += '?' + qs.stringify(options.bod)
    delete options.body;
  }


  return fetch(path, options).then(res => res.json()).catch(err => {
    console.error(err)
  })
}
